/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package AdminExpo;

import JavaClass.ActionAll;
import Main.ExpoManagerMain;
import check.check;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Vector;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JComboBox;
import javax.swing.JOptionPane;
import javax.swing.JPasswordField;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.table.DefaultTableModel;

/**
 *
 * @author Mai Lan
 */
public class AdminAction {
    public static  void LoadAd(JTable tb)  throws SQLException{
          Vector admincp=new Vector();
          Vector adminfeil=new Vector();

           admincp.clear();
                admincp.add("IdAd");
                admincp.add("NameAd");
                admincp.add("AccountAd");
                admincp.add("PassWordAd");
                admincp.add("EmailAd");
                admincp.add("powerAd");
           adminfeil.clear();
               adminfeil.add("ID");
               adminfeil.add("NameAd");
               adminfeil.add("AccountAd");
               adminfeil.add("PassWordAd");
               adminfeil.add("EmailAd");
               adminfeil.add("powerAd");

          ActionAll.LoadInfor(admincp, tb, adminfeil, "AdminExpo");


    }
    public static  void insertAd(JTable tb,JTextField txtAccAd,JPasswordField passPW,JTextField  txtEmail,JTextField txtNameAd,JComboBox cobSuperAd) { 
            boolean checkReady=check.checkReadlyData("AdminExpo","AccountAd" , txtAccAd, ExpoManagerMain.conn);
                if(checkReady==false){
                    return;

                }else{

                    try {

                        String sqlInsert = "Insert into AdminExpo (AccountAd,PassWordAd,EmailAd,NameAd,powerAd) values(?,?,?,?,?)";
                        PreparedStatement psInsert = ExpoManagerMain.conn.prepareStatement(sqlInsert);
                        psInsert.setString(1, txtAccAd.getText());
                        psInsert.setString(2, new String(passPW.getPassword()));
                        psInsert.setString(3, txtEmail.getText());
                        psInsert.setString(4, txtNameAd.getText());
                        psInsert.setString(5, (String)cobSuperAd.getSelectedItem());
                        psInsert.executeUpdate();


                         AdminAction.LoadAd(tb);
                         
                    } catch (SQLException ex) {
                        Logger.getLogger(AdminManage.class.getName()).log(Level.SEVERE, null, ex);
                    }


        }

        try {
           AdminAction.LoadAd(tb);
        } catch (SQLException ex) {
            Logger.getLogger(AdminManage.class.getName()).log(Level.SEVERE, null, ex);
        }
        

    }
       public static  void updateAd(JTable tb,JTextField txtAccAd,JPasswordField passPW,JTextField  txtEmail,JTextField txtNameAd,JComboBox cobSuperAd)throws SQLException{
           
           int n=tb.getSelectedRow();
           if(tb.isCellSelected(tb.getSelectedRow(), tb.getSelectedColumn())==false){
                JOptionPane.showMessageDialog(null, "Please choice admin want to update!!");

           }else {



                    try {

                        int Id;
                        Id = Integer.parseInt((String) tb.getValueAt(n, 0));
                          Statement stUp = ExpoManagerMain.conn.createStatement();
                          String sqlUpAd="Update AdminExpo Set NameAd='"+txtNameAd.getText().trim()+"',AccountAd='"+
                          txtAccAd.getText().trim()+"',PassWordAd='"+new String(passPW.getPassword())+"',EmailAd='"+
                          txtEmail.getText().trim()+"' ,powerAd='"+(String)cobSuperAd.getSelectedItem()+
                              "'  where ID="+Id;

                         stUp.executeUpdate(sqlUpAd);


                         AdminAction.LoadAd(tb);
                          
                    } catch (SQLException ex) {
                        Logger.getLogger(AdminManage.class.getName()).log(Level.SEVERE, null, ex);
                    }
                    AdminAction.LoadAd(tb);

           }


    }
    public  static  void deleteAd(JTable tbAd) throws SQLException{
        ActionAll.deleteInfor(tbAd, "AdminExpo", "Id");
        
        AdminAction.LoadAd(tbAd);
               
            

    }
}
